#include "../../afwApp.h"

#include <GL/glut.h>

using namespace one_rag::App::impl;

GlutApp* GlutApp::_self = 0;

GlutApp::GlutApp():_appDelegate(0)
{
  assert(!_self);
  _self = this;
}

GlutApp::~GlutApp(){
  assert(_self);
  _self=0;
}

void GlutApp::Init(int* argc, char** argv)
{
  glutInit(argc,argv);
}

void GlutApp::MainLoop()
{
  glutMainLoop();
}

void GlutApp::SetDelegate(GlutAppDelegate* appDelegate,unsigned int glutAppCallbackFlags)
{
  assert(!_appDelegate || !appDelegate);
  if( (_appDelegate = appDelegate) )
  {
    if(glutAppCallbackFlags & GlutAppCallbackIdle)
      glutIdleFunc(&idleFunc);
  }
}

void GlutApp::SetTimer(int msec,int timerId)
{
  glutTimerFunc(msec,&timerFunc,timerId);
}

void GlutApp::timerFunc(int timerId){
  if(_self && _self->_appDelegate)
    _self->_appDelegate->OnTimer(timerId);
}

void GlutApp::idleFunc(){
  if(_self && _self->_appDelegate)
    _self->_appDelegate->OnIdle();
}



